package top.whysu.edevice.controller.manage;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.whysu.edevice.dto.generate.RequestDTO;
import top.whysu.edevice.dto.generate.ResponseDTO;
import top.whysu.edevice.dto.manage.NodeAddDTO;
import top.whysu.edevice.dto.manage.NodeEditDTO;
import top.whysu.edevice.po.manage.ManageTreePO;
import top.whysu.edevice.service.manage.ManageTreeService;
import top.whysu.edevice.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 树结构
 */
@Controller
@RequestMapping("/manageTree")
public class ManageTreeController {

    @Resource
    private ManageTreeService manageTreeService;

    /**
     * 获取树结构
     */
    @PostMapping("/selectTreeNodeByCondition")
    @ResponseBody
    public ResponseDTO<List<ManageTreePO>> selectTreeNodeByCondition(@RequestBody @Validated RequestDTO requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageTreeService.selectTreeNodeByCondition(new HashMap<>(1)));
    }

    /**
     * 添加节点
     */
    @PostMapping("/addNode")
    @ResponseBody
    public ResponseDTO<BigDecimal> addNode(@RequestBody @Validated RequestDTO<NodeAddDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageTreeService.wholeAddNode(requestDTO.getBody()));
    }

    /**
     * 编辑节点
     */
    @PostMapping("/editNode")
    @ResponseBody
    public ResponseDTO editNode(@RequestBody @Validated RequestDTO<NodeEditDTO> requestDTO) {
        manageTreeService.wholeModifyNode(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

    /**
     * 删除节点
     */
    @PostMapping("/deleteNode")
    @ResponseBody
    public ResponseDTO deleteNode(@RequestBody RequestDTO<BigDecimal> requestDTO) {
        manageTreeService.wholeDeleteNode(requestDTO.getBody());
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader());
    }

}